package com.example.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.AfterSalefront;
import com.example.demo.entity.AfterSalefront2;
import com.example.demo.entity.CarKeep;
import com.example.demo.entity.CarKeep2;
import com.example.demo.entity.CarNumberCompany1;
import com.example.demo.entity.CarNumberCompany2;
import com.example.demo.entity.Insurance;
import com.example.demo.entity.MoneyCompany;
import com.example.demo.entity.SaleBoutique1;
import com.example.demo.entity.SaleBoutique2;
import com.example.demo.entity.SysUser;
import com.example.demo.entity.XiMei;
import com.example.demo.entity.XiMei2;
import com.example.demo.entity.YanBao;
import com.example.demo.entity.YanBao2;
import com.example.demo.form.AfterSalefrontForm;
import com.example.demo.security.UserUtils;
import com.example.demo.service.AfterSalefront2Service;
import com.example.demo.service.AfterSalefrontService;
import com.example.demo.service.CarKeep2Service;
import com.example.demo.service.CarKeepService;
import com.example.demo.service.CarNumberCompanyService1;
import com.example.demo.service.CarNumberCompanyService2;
import com.example.demo.service.InsuranceService;
import com.example.demo.service.NewCar1Service;
import com.example.demo.service.SaleBoutique1Service;
import com.example.demo.service.SaleBoutique2Service;
import com.example.demo.service.SysUserService;
import com.example.demo.service.XiMei2Service;
import com.example.demo.service.XiMeiService;
import com.example.demo.service.YanBao2Service;
import com.example.demo.service.YanBaoService;

@RestController
@RequestMapping(value = "/afterSalefront2")
public class AfterSalefront2Controller {
	@Autowired
	private NewCar1Service carService;
	@Autowired
	private AfterSalefront2Service service2;
	@Autowired
	private AfterSalefrontService service1;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysUserService sysuserService;

	@Autowired
	private SaleBoutique1Service saleBoutique1DAO;

	@Autowired
	private CarNumberCompanyService1 carnumberService1;


	@Autowired
	private CarNumberCompanyService2 carnumberService2;

	@Autowired
	private SaleBoutique2Service saleBoutique2DAO;

	@Autowired
	private CarKeepService carKeepService1;
	@Autowired
	private CarKeep2Service carKeepService2;

	@Autowired
	private YanBao2Service yanBaoService2;
	@Autowired
	private YanBaoService yanBaoService1;

	@Autowired
	private XiMei2Service xiMeiService2;
	@Autowired
	private XiMeiService xiMeiService1;

	@Autowired
	private InsuranceService insuranceDAO;

	private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/save")
	private Object save2(AfterSalefrontForm form) throws ParseException {
		Integer saleId1 = form.getId();
		Date d = new Date();
		Float xiao = (float) 0;
		SysUser user = userUtils.getUser();
		Date d1 = dateFormat1.parse("1999-01-01");
		AfterSalefront sale1 = new AfterSalefront();
		AfterSalefront Sale1 = new AfterSalefront();
		AfterSalefront2 sale2 = new AfterSalefront2();
		AfterSalefront2 Sale2 = new AfterSalefront2();
		if (saleId1 == null) {
			Sale1.setChePaiNum(form.getChePaiNum());
			Sale1.setSysuser(user);
			Sale1.setJishi(sysuserService.findById(form.getWeixiuPerson()));
			sale1 = service1.save(Sale1);
		} else {
			sale1 = service1.findById(saleId1);
			sale1.setChePaiNum(form.getChePaiNum());
			Sale1.setSysuser(user);
			sale1.setJishi(sysuserService.findById(form.getWeixiuPerson()));
			service1.save(sale1);
			List<AfterSalefront2> list = service2.findByAfterSalefront(sale1);
			for (AfterSalefront2 AfterSalefront2 : list) {
				if (d1.compareTo(AfterSalefront2.getBeginDate()) == -1) {
					d1 = AfterSalefront2.getBeginDate();
					Sale2 = AfterSalefront2;
				}
			}
			Sale2.setEndDate(d);
			service2.save(Sale2);

		}
		// 售精品
		if (form.getMaiJingPin() != null && form.getMaiJingPin().length() > 0) {
			List<SaleBoutique1> model = new ArrayList<>();
			String model2 = form.getMaiJingPin();
			for (String name : model2.split(",")) {
				model.add(saleBoutique1DAO.findById(Integer.parseInt(name)));
				List<SaleBoutique2> list = saleBoutique2DAO
						.findBySaleBoutique(saleBoutique1DAO.findById(Integer.parseInt(name)));
				SaleBoutique2 sale = new SaleBoutique2();
				Integer id = 0;
				for (SaleBoutique2 y : list) {
					if (id < y.getId()) {
						id = y.getId();
						sale = y;
					}
				}
				xiao += sale.getCaiLiaoPrice() + sale.getGongShiPrice();
			}
			sale2.setMaiJingPin(model);
		}

		// 售保养
		if (form.getMaiBaoYang() != null && form.getMaiBaoYang().length() > 0) {
			List<CarKeep> model = new ArrayList<>();
			String model2 = form.getMaiBaoYang();
			for (String name : model2.split(",")) {
				model.add(carKeepService1.findById(Integer.parseInt(name)));
				List<CarKeep2> list = carKeepService2.findByCarKeep(carKeepService1.findById(Integer.parseInt(name)));
				CarKeep2 d11 = new CarKeep2();
				Integer id = 0;
				for (CarKeep2 y : list) {
					if (id < y.getId()) {
						id = y.getId();
						d11 = y;
					}
				}
				xiao += d11.getPrice();
			}
			sale2.setMaiBaoYang(model);
		}

		// 延保
		if (form.getMaiYanBao() != null && form.getMaiYanBao().length() > 0) {
			List<YanBao> model = new ArrayList<>();
			String model2 = form.getMaiYanBao();
			for (String name : model2.split(",")) {
				model.add(yanBaoService1.findById(Integer.parseInt(name)));
				List<YanBao2> list = yanBaoService2.findByYanBaoId(Integer.parseInt(name));
				YanBao2 a = new YanBao2();
				Integer id = 0;
				for (YanBao2 y : list) {
					if (id < y.getId()) {
						id = y.getId();
						a = y;
					}
				}
				xiao += a.getYanBaoPrice();
			}
			sale2.setMaiYanBao(model);
		}

		// 洗美
		if (form.getMaiXiMei() != null && form.getMaiXiMei().length() > 0) {
			List<XiMei> model = new ArrayList<>();
			String model2 = form.getMaiXiMei();
			for (String name : model2.split(",")) {
				model.add(xiMeiService1.findById(Integer.parseInt(name)));
				List<XiMei2> list = xiMeiService2.findByXiMeiId(Integer.parseInt(name));
				XiMei2 a = new XiMei2();
				Integer id = 0;
				for (XiMei2 y : list) {
					if (id < y.getId()) {
						id = y.getId();
						a = y;
					}
				}
				xiao += a.getCaiLiaoPrice() + a.getGongShiPrice();
			}
			sale2.setMaiXiMei(model);
		}

		if (form.getMaiBaoXian() != null && form.getMaiBaoXian().length() > 0) {
			List<Insurance> model = new ArrayList<>();
			model.add(insuranceDAO.findById(Integer.parseInt(form.getMaiBaoXian())));
			sale2.setMaiBaoXian(model);
		}
		if (form.getMaiChePai()!= null && form.getMaiChePai().length() > 0) {
			List<CarNumberCompany1> model = new ArrayList<>();
			model.add(carnumberService1.findById(Integer.parseInt(form.getMaiChePai())));
			sale2.setMaiChePai(model);
		}

		xiao += form.getMaiJiaoQiang();
		xiao += form.getMaiShangYeXian();
		Integer xiao1 = xiao.intValue();

		BeanUtils.copyProperties(form, sale2, "id", "chePaiNum");
		sale2.setAfterSalefront(sale1);
		sale2.setBeginDate(d);
		service2.save(sale2);
		return new AjaxResult("数据保存成功");
	}

	@RequestMapping(value = "/findByUserId")
	public Object findByUserId(AfterSalefrontForm Form) {
		List<AfterSalefront> thread = new ArrayList<AfterSalefront>();
		Integer id = Form.getUserId();
		if (id != null) {
			SysUser user = sysuserService.findById(id);
			thread = service1.findBySysuser(user);
		}

		return new AjaxResult(thread);
	}

	@RequestMapping(value = "/findbyId")
	public Object findbyId(Integer id) throws ParseException {
		AfterSalefront after = service1.findById(id);
		List<AfterSalefront2> list = service2.findByAfterSalefront(after);
		DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		Date d = dateFormat1.parse("1999-01-01");
		AfterSalefront2 yan = new AfterSalefront2();
		for (AfterSalefront2 y : list) {
			if (d.compareTo(y.getBeginDate()) == -1) {
				d = y.getBeginDate();
				yan = y;
			}
		}
		return new AjaxResult(yan);
	}

}
